package com.ysd.lis.service.sta.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.sta.StaWorkFlow;
import com.ysd.lis.mapper.sta.StaWorkFlowMapper;
import com.ysd.lis.service.sta.StaWorkFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 流程设定主表 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-04-09
 */
@Service
public class StaWorkFlowServiceImpl extends ServiceImpl<StaWorkFlowMapper, StaWorkFlow> implements StaWorkFlowService {
    @Autowired
    StaWorkFlowMapper bacWorkFlowMapper;
    // @Autowired
    // BacWorkFlowNodeDictMapper bacWorkFlowNodeDictMapper;
    // @Autowired
    // BacWorkFlowNodeMapper bacWorkFlowNodeMapper;

    @Override
    public Result findBacWorkFlowList(StaWorkFlow bacWorkFlow) {
        List<StaWorkFlow> list = findBacWorkFlowArray(bacWorkFlow);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<StaWorkFlow> findBacWorkFlowArray(StaWorkFlow bacWorkFlow) {
        MPJLambdaWrapper<StaWorkFlow> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(StaWorkFlow::getDelFlag, 0);
        queryWrapper.eq(StringUtils.isNotBlank(bacWorkFlow.getId()), StaWorkFlow::getId, bacWorkFlow.getId());
        return bacWorkFlowMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacWorkFlow(StaWorkFlow bacWorkFlow) {
        int i = bacWorkFlowMapper.insert(bacWorkFlow);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacWorkFlow);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacWorkFlow(StaWorkFlow bacWorkFlow) {
        bacWorkFlow.setDelFlag(1);
        int i = bacWorkFlowMapper.updateById(bacWorkFlow);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacWorkFlow);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacWorkFlow(StaWorkFlow bacWorkFlow) {
        int i = bacWorkFlowMapper.updateById(bacWorkFlow);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacWorkFlow);
        }
        return Result.fail("更新失败");
    }

    // @Override
    // public Result selectProsettingNode(String settingId) {
    //     //查询出中间表的节点信息
    //     Map map = new HashMap<>();
    //     LambdaQueryWrapper<BacWorkFlowNode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    //     lambdaQueryWrapper.eq(BacWorkFlowNode::getFlowNo,settingId);
    //     List<BacWorkFlowNode> bacWorkFlowNodes = bacWorkFlowNodeMapper.selectList(lambdaQueryWrapper);
    //     List<String> collect = bacWorkFlowNodes.stream().map(BacWorkFlowNode::getNodeDictNo).collect(Collectors.toList());
    //     if(CollectionUtils.isNotEmpty(collect)){
    //         LambdaQueryWrapper<BacWorkFlowNodeDict> lambdaQueryWrapper1 = new LambdaQueryWrapper();
    //         lambdaQueryWrapper1.in(BacWorkFlowNodeDict::getCode,collect).orderByAsc(BacWorkFlowNodeDict::getSeq);
    //         List<BacWorkFlowNodeDict> bacWorkFlowNodeDicts = bacWorkFlowNodeDictMapper.selectList(lambdaQueryWrapper1);
    //         LambdaQueryWrapper<BacWorkFlowNodeDict> lambdaQueryWrapper2 = new LambdaQueryWrapper();
    //         lambdaQueryWrapper2.notIn(BacWorkFlowNodeDict::getCode,collect);
    //         lambdaQueryWrapper2.orderByAsc(BacWorkFlowNodeDict::getSeq);
    //
    //         List<BacWorkFlowNodeDict> bacWorkFlowNodeDicts1 = bacWorkFlowNodeDictMapper.selectList(lambdaQueryWrapper2);
    //         map.put("workFlowList", bacWorkFlowNodeDicts);
    //         map.put("workFlowNodeList", bacWorkFlowNodeDicts1);
    //     } else {
    //         List<BacWorkFlowNodeDict> bacWorkFlowNodeDicts = bacWorkFlowNodeDictMapper.selectList(new LambdaQueryWrapper<BacWorkFlowNodeDict>().orderByAsc(BacWorkFlowNodeDict::getSeq));
    //         map.put("workFlowList", new ArrayList<>());
    //         map.put("workFlowNodeList", bacWorkFlowNodeDicts);
    //     }
    //     return Result.succ(map);
    // }

    @Override
    public StaWorkFlow findOneByNo(String no) {
        if (StringUtils.isBlank(no)) {
            return null;
        }
        LambdaQueryWrapper<StaWorkFlow> query = new LambdaQueryWrapper<>();
        query.eq(StaWorkFlow::getDelFlag, 0);
        query.eq(StaWorkFlow::getWorkFlowCode, no);
        List<StaWorkFlow> lst = bacWorkFlowMapper.selectList(query);
        if (!lst.isEmpty()) {
            return lst.get(0);
        }
        return null;
    }
}
